const state = {
  searchExp: '', // 输入框检索式
  secondSearchExp: '', // 二筛输入的检索式
  filterAndExcludeExp: '', // 处理完筛选后的检索式
  companyExp: '', // 企业名录的检索式
  tempFilterItem: {}, // 临时的侧边栏存储
  filterSelected: {}, // 临时的侧边栏存储（过滤
  excludeSelected: {}, // 临时的侧边栏存储（排除
  forbiddenMap: [], // 禁用白名单
  hasSearch: [], // 当前已经发起过请求的侧边栏
  companySelectedList: [] // 企业名录已选
}
const getters = {
  getSearchExper: (state) => {
    const {
      filterAndExcludeExp, companyExp, searchExp, secondSearchExp
    } = state
    const expList = [searchExp, secondSearchExp, companyExp].filter((item) => Boolean(item))
    return `${expList.join(' and ')}${filterAndExcludeExp}`
  },
  getFilterSelected: (state) => state.filterSelected, // 获取当前过滤已选
  getExcludeSelected: (state) => state.excludeSelected, // 获取当前已排除
  getFilterAndExcludeExp: (state) => state.filterAndExcludeExp, // 处理完排除和筛选的检索式
  getCompanyExp: (state) => state.companyExp, // 获取企业名录的检索式
  hasSearchMap: (state) => state.hasSearch,
  getCompanySelectedList: (state) => state.companySelectedList, // 返回当前企业名录已选项
  getSecondSearchExp: (state) => state.secondSearchExp  // 返回二次检索式
}
const mutations = {
  // 更新
  updateTempFilterItem(state, params) {
    const {
      label, selectedValue, customValue, isCustomDeal
    } = params
    if (isCustomDeal) { // 自定义处理
      if (customValue.length === 0) return this.commit('filterGroup/deleteTempFilterItem', label)
    } else if (selectedValue.length === 0) {
      return this.commit('filterGroup/deleteTempFilterItem', label)
    }
    state.tempFilterItem[label] = { ...params }
  },
  // 更新已发请求的名单
  updateHasSearchMap(state, uniqueId) {
    state.hasSearch.push(uniqueId)
  },
  // 过滤调已发请求map中的目标
  filterHasRequestTarget(state, target) {
    state.hasSearch = state.hasSearch.filter((item) => item !== target)
  },
  // 单独删除
  deleteTempFilterItem(state, label) {
    delete state.tempFilterItem[label]
  },
  // 清空临时勾选缓存
  clearTempFilterItem(state) {
    state.tempFilterItem = {}
  },
  // 清空检索式结果
  clearFilterAndExcludeExp(state) {
    state.filterAndExcludeExp = ''
  },
  // 清空hasSearch表
  clearHasSearchMap(state) {
    state.hasSearch = []
  },
  // 清空企业名录的检索式
  clearCompanyExp(state) {
    state.companyExp = ''
  },
  // 清空二筛输入框的值
  clearSearchExp(state) {
    state.searchExp = ''
  },
  // 清空而筛侧边栏输入框的检索式
  clearSecondSearchExp(state) {
    state.secondSearchExp = ''
  },
  // 清空企业名录已选
  clearCompanySelectedList(state) {
    state.companySelectedList = []
  },
  // 更新企业名录已选
  updateCompanySelectedList(state, v) {
    state.companySelectedList = v
  },
  // 重置所有的配置
  resetAllSetting(state) {
    state.tempFilterItem = {}
    state.filterSelected = {}
    state.excludeSelected = {}
  },
  // 移除筛选项 or 已选项
  clearFilterOrExcludeSelected(state, params) {
    const {
      label, type, target, parent
    } = params
    if (type === 'clearItem') { // 单独删除
      const curInstance = state[target][parent]
      const { isCustomDeal, customValue, selectedValue } = curInstance
      if (isCustomDeal) { // 如果是归属于自定义的值的类型，则由于字段的不同，需要单独处理
        state[target][parent].customValue = customValue.filter((item) => item.indexOf(label) === -1)
        if (curInstance.customValue.length === 0) {
          delete state[target][parent]
        }
      } else { // 一般情况下的处理 !! 待优化，应该让他们一致
        state[target][parent].selectedValue = selectedValue.filter((item) => item.indexOf(label) === -1)
        if (curInstance.selectedValue.length === 0) {
          delete state[target][parent]
        }
      }
    } else { // 整体删除
      this._vm.$set(state[target], label, null)
      delete state[target][label]
    }
    // 更新检索式
    this.commit('filterGroup/dealExp')
  },
  // 点击了筛选or排除
  filterOrExcludeExpHandler(state, type) {
    const tempList = state.tempFilterItem
    this.commit('filterGroup/classifyFilterOrExclude', { type, tempList }) // 区分临时保存的结果
    // this.commit('filterGroup/clearTempFilterItem') // 清空之前的勾选结果
    this.commit('filterGroup/dealExp', type) // 处理检索式
  },
  // 分类不同类型下的结果
  classifyFilterOrExclude(state, params) {
    const { type, tempList } = params
    if (type === 'filter') {
      state.filterSelected = { ...state.filterSelected, ...tempList }
    } else {
      state.excludeSelected = { ...state.excludeSelected, ...tempList }
    }
  },
  // 处理检索式结果
  dealExp(state) {
    const typeSelected = [state.filterSelected, state.excludeSelected] // 0:筛选， 1：排除
    const expResult = []
    typeSelected.forEach((item, index) => {
      const exp = []
      Object.keys(item).forEach((key) => {
        const {
          expName, selectedValue, isCustomDeal, customValue
        } = item[key]
        if (isCustomDeal) { // 内部自定义处理完毕的结果
          customValue.forEach((item) => exp.push(item))
        } else {
          exp.push(`${expName}=(${selectedValue.join(' or ')})`)
        }
      })
      if (exp.length > 0) { // 不为空才处理
        const extJoin = exp.join(' and ')
        const prefix = index === 0 ? ' and ' : ' not ' // 0 是筛选， 1: 才是排除
        expResult.push(`${prefix}(${extJoin})`)
      }
    })
    state.filterAndExcludeExp = expResult.join('')
  },
  // 企业名录的检索式处理
  updateCompany(state, exp) {
    state.companyExp = exp
  },
  // 筛选检索式输入
  updateSearchExp(state, exp) {
    state.searchExp = exp
  },
  // 侧边栏的二次筛选的检索式输入
  updateSecondSearchExp(state, exp) {
    state.secondSearchExp = exp
  }
}
const actions = {}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}